<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 68KB
 *
 * An open source knowledge base script
 *
 * @package		68kb
 * @author		68kb Dev Team
 * @copyright	Copyright (c) 2009, 68 Designs, LLC
 * @license		http://68kb.com/user_guide/license.html
 * @link		http://68kb.com
 * @since		Version 1.0
 */

// ------------------------------------------------------------------------

/**
 * KB Controller
 *
 * Main Setup Controller
 *
 * @package		68kb
 * @subpackage	Migrations
 * @category	Migrations
 * @author		68kb Dev Team
 * @link		http://68kb.com/
 * @version 	$Id: kb.php 125 2009-11-15 05:15:57Z suzkaw68 $
 */
class Create_core
{
	// ------------------------------------------------------------------------

	function up()
	{
		$CI =& get_instance();
		$CI->load->dbforge();
		$output = '';
		if ($CI->core_migrate->verbose)
		{
			$output = "Creating core tables...<br />";
		}

		$class_methods = get_class_methods($this);
		foreach ($class_methods as $method_name)
		{
			if(substr($method_name, 0, 6) == 'table_')
			{
				$output .= $this->$method_name();
			}
		}
		$this->default_data();

		return $output;
	}

	// ------------------------------------------------------------------------

	function down()
	{
		$CI =& get_instance();

		$CI->load->dbforge();

		if ($CI->core_migrate->verbose)
		{
			echo "Dropping core tables...";
		}
		$tables = $CI->db->list_tables();
		foreach($tables as $table)
		{
			$table = str_replace($CI->db->dbprefix, '', $table);
			if ($CI->dbforge->drop_table($table))
			{
				echo 'Dropping table '. $table.'<br />';
			}
		}
		return TRUE;
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Install Articles Table
	*/
	function table_articlestocat()
	{
		$CI =& get_instance();
		
		if ( ! $CI->db->table_exists('article2cat'))
		{	
			$CI->dbforge->add_field("article_id int(20) default NULL");
			$CI->dbforge->add_field("category_id int(20) default NULL");

			$CI->dbforge->add_key('article_id', TRUE);
			$CI->dbforge->add_key('category_id', TRUE);
			if ($CI->dbforge->create_table('article2cat'))
			{
				return 'article2cat table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Install Articles Table
	*/
	function table_articles()
	{
		$CI =& get_instance();
		
		if ( ! $CI->db->table_exists('article2cat'))
		{
			$fields = array(
					'article_id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("article_uri varchar(55) NOT NULL default '0'");
			$CI->dbforge->add_field("article_title varchar(255) NOT NULL default ''");
			$CI->dbforge->add_field("article_keywords varchar(255) NOT NULL default ''");
			$CI->dbforge->add_field("article_description text NOT NULL");
			$CI->dbforge->add_field("article_short_desc text NOT NULL");
			$CI->dbforge->add_field("article_date int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("article_modified int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("article_display char(1) NOT NULL default 'N'");
			$CI->dbforge->add_field("article_hits int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("article_author int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("article_order int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("article_rating int(11) NOT NULL default '0'");
			$CI->dbforge->add_key('article_id', TRUE);
			$CI->dbforge->add_key('article_uri', TRUE);
			$CI->dbforge->add_key('article_title', TRUE);
			if ($CI->dbforge->create_table('articles'))
			{
				return 'articles table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Install Attachments Table
	*/
	function table_attachments()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('attachments'))
		{
			$fields = array(
					'attach_id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("article_id int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("attach_name varchar(55) NOT NULL default ''");
			$CI->dbforge->add_field("attach_type varchar(55) NOT NULL default ''");
			$CI->dbforge->add_field("attach_size varchar(55) NOT NULL default ''");
			$CI->dbforge->add_key('attach_id', TRUE);
			if ($CI->dbforge->create_table('attachments'))
			{
				return 'attachments table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	* Install Captcha Table
	*/
	function table_captcha()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('captcha'))
		{
			$fields = array(
					'captcha_id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("captcha_time int(10) NOT NULL default '0'");
			$CI->dbforge->add_field("ip_address varchar(16) NOT NULL default '0'");
			$CI->dbforge->add_field("word varchar(20) NOT NULL default ''");
			$CI->dbforge->add_field("a_size varchar(255) NOT NULL default ''");
			$CI->dbforge->add_key('captcha_id', TRUE);
			$CI->dbforge->add_key('word');
			if($CI->dbforge->create_table('captcha'))
			{
				return 'captcha table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Install Categories
	*/
	function table_categories()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('categories'))
		{
			$fields = array(
					'cat_id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("cat_parent int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("cat_uri varchar(55) NOT NULL default '0'");
			$CI->dbforge->add_field("cat_name varchar(255) NOT NULL default ''");
			$CI->dbforge->add_field("cat_description text NOT NULL");
			$CI->dbforge->add_field("cat_display char(1) NOT NULL DEFAULT 'N'");
			$CI->dbforge->add_field("cat_order int(11) NOT NULL default '0'");
			$CI->dbforge->add_key('cat_id', TRUE);
			$CI->dbforge->add_key('cat_uri', TRUE);
			$CI->dbforge->add_key('cat_name');
			$CI->dbforge->add_key('cat_parent');
			$CI->dbforge->add_key('cat_order');
			if($CI->dbforge->create_table('categories'))
			{
				return 'categories table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Install Comments Table
	*/
	function table_comments()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('comments'))
		{
			$fields = array(
					'comment_ID' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("comment_article_ID int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("comment_author varchar(55) NOT NULL default ''");
			$CI->dbforge->add_field("comment_author_email varchar(55) NOT NULL default ''");
			$CI->dbforge->add_field("comment_author_IP varchar(16) NOT NULL default ''");
			$CI->dbforge->add_field("comment_date int(16) NOT NULL default '0'");
			$CI->dbforge->add_field("comment_content text NOT NULL");
			$CI->dbforge->add_field("comment_approved enum('0','1','spam') NOT NULL default '1'");
			$CI->dbforge->add_key('comment_ID', TRUE);
			$CI->dbforge->add_key('comment_article_ID');
			if($CI->dbforge->create_table('comments'))
			{
				return 'comments table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Install Glossary Table
	*/
	function table_glossary()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('glossary'))
		{
			$fields = array(
					'g_id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("g_term varchar(55) NOT NULL default ''");
	        $CI->dbforge->add_field("g_definition text NOT NULL");
			$CI->dbforge->add_key('g_id', TRUE);
			$CI->dbforge->add_key('g_term');
			if($CI->dbforge->create_table('glossary'))
			{
				return 'glossary table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Install Modules Table
	*/
	function table_modules()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('modules'))
		{
			$fields = array(
					'id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("name varchar(255) NOT NULL default ''");
	        $CI->dbforge->add_field("displayname varchar(255) NOT NULL default ''");
	        $CI->dbforge->add_field("description varchar(255) NOT NULL default ''");
	        $CI->dbforge->add_field("directory varchar(255) NOT NULL default ''");
	        $CI->dbforge->add_field("version varchar(10) NOT NULL default ''");
	        $CI->dbforge->add_field("active tinyint(1) NOT NULL default '0'");
	        $CI->dbforge->add_key('id', TRUE);
			$CI->dbforge->add_key('name', TRUE);
			$CI->dbforge->add_key('displayname');
			$CI->dbforge->add_key('active');
		
			if($CI->dbforge->create_table('modules'))
			{
				return 'modules table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Install Search Log Table
	*/
	function table_searchlog()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('searchlog'))
		{
			$fields = array(
					'searchlog_id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("searchlog_term varchar(55) NOT NULL default ''");
	        $CI->dbforge->add_key('searchlog_id', TRUE);
	        $CI->dbforge->add_key('searchlog_term');
			if($CI->dbforge->create_table('searchlog'))
			{
				return 'searchlog table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	* Install Sessions Table
	*/
	function table_sessions()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('sessions'))
		{
			$fields = array(
					'session_id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("ip_address varchar(16) NOT NULL default '0'");
			$CI->dbforge->add_field("user_agent varchar(50) NOT NULL default ''");
			$CI->dbforge->add_field("last_activity int(10) NOT NULL default '0'");
	        $CI->dbforge->add_key('session_id', TRUE);
			if($CI->dbforge->create_table('sessions'))
			{
				return 'sessions table installed...<br />';
			}
		}
	}

	// ------------------------------------------------------------------------

	/**
	* Install Settings table
	*/
	function table_settings()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('settings'))
		{
			$fields = array(
					'option_id' => array( 'type' => 'INT', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE ),
	                'short_name' => array( 'type' => 'VARCHAR', 'constraint' => '55' ),
					'name' => array( 'type' => 'VARCHAR', 'constraint' => '255' ),
					'value' => array( 'type' => 'VARCHAR', 'constraint' => '255' ),
					'auto_load' => array( 'type' => 'VARCHAR', 'constraint' => '16' )
			);

			$CI->dbforge->add_field($fields);

			$CI->dbforge->add_key('option_id', TRUE);
			$CI->dbforge->add_key('short_name', TRUE);
			$CI->dbforge->add_key('value');
			$CI->dbforge->add_key('auto_load');

			$CI->dbforge->create_table('settings');

			$data = array('short_name' => 'site_name','name' => "Site Title",'value' => 'Your Site','auto_load' => 'yes');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'site_email','name' => "Site Email",'value' => KB_EMAIL,'auto_load' => 'yes');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'version','name' => "Script Version",'value' => KB_VERSION,'auto_load' => 'yes');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'last_cron','name' => "Last Cron",'value' => "",'auto_load' => 'yes');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'template','name' => "Template",'value' => 'default','auto_load' => 'yes');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'admin_template','name' => "Admin Template",'value' => 'default','auto_load' => 'yes');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'max_search','name' => "Per Page",'value' => '5','auto_load' => 'yes');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'cache_time','name' => "Cache Time",'value' => '0','auto_load' => 'yes');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'comments','name' => "Allow Comments",'value' => 'Y','auto_load' => 'yes');
			$CI->db->insert('settings', $data);		
			$data = array('short_name' => 'latest','name' => "Latest 68KB Release",'value' => '0','auto_load' => 'no');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'site_keywords','name' => "Site Keywords",'value' => 'keywords, go, here','auto_load' => 'yes');
			$CI->db->insert('settings', $data);
			$data = array('short_name' => 'site_description','name' => "Site Description",'value' => 'Site Description','auto_load' => 'yes');
			$CI->db->insert('settings', $data);

			return 'settings table installed...<br />';
		}
	}

	// ------------------------------------------------------------------------
	
	/**
	* Install Users Table
	*/
	function table_users()
	{
		$CI =& get_instance();

		if ( ! $CI->db->table_exists('users'))
		{
			$fields = array(
					'id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE),
			);
			$CI->dbforge->add_field($fields);
			$CI->dbforge->add_field("custip varchar(16) NOT NULL default '0'");
			$CI->dbforge->add_field("firstname varchar(50) NOT NULL default ''");
			$CI->dbforge->add_field("lastname varchar(50) NOT NULL default ''");
			$CI->dbforge->add_field("email varchar(50) NOT NULL default ''");
			$CI->dbforge->add_field("username varchar(50) NOT NULL default ''");
			$CI->dbforge->add_field("password varchar(50) NOT NULL default ''");
			$CI->dbforge->add_field("joindate int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("lastlogin int(11) NOT NULL default '0'");
			$CI->dbforge->add_field("cookie varchar(50) NOT NULL default ''");
			$CI->dbforge->add_field("session varchar(50) NOT NULL default ''");
			$CI->dbforge->add_field("level int(5) NOT NULL default '5'");
	        $CI->dbforge->add_key('id', TRUE);
			if($CI->dbforge->create_table('users'))
			{
				return 'users table installed...<br />';
			}
		}
	}
	
	// ------------------------------------------------------------------------
	
	function default_data()
	{
		$CI =& get_instance();
		$data = array(
				'name' => 'jwysiwyg',
				'displayname' => 'WYSIWYG jQuery Plugin',
				'description' => 'This module is an inline content editor to allow editing rich HTML content on the fly.',
				'directory' => 'jwysiwyg',
				'version' => 'v1.0',
				'active' => '1'
			);
		$CI->db->insert('modules', $data);
		$data = array(
					'article_author' => 1,
					'article_title' => "Welcome to 68kb",
					'article_uri' => "welcome",
					'article_keywords' => 'cat',
					'article_short_desc' => 'Short Description',
					'article_description' => 'Welcome to 68kb!<div><br></div><div>Thank you for downloading and installing 68kb. I know as with any script it probably does things differently than others and at CI time we would like to highlight some of the available resources available to you.&nbsp;</div><div><br></div><div>1.&nbsp;<a href="http://68kb.com/knowledge-base/">Knowledge Base</a>&nbsp;- Our knowledge base includes information about using and working with the script.&nbsp;</div><div>2. <a href="http://68kb.com/support/">Support Forums</a> - We have community support forums where you can get help, advice, or talk with other 68kb users.&nbsp;</div><div>3. <a href="http://68kb.com/blog/">68kb Blog</a> - Our blog covers new releases and other tips and tricks to get you comfortable with 68kb.&nbsp;</div><div><br></div><div>Thanks again!</div>',
					'article_display' => 'Y',
					'article_date' => time(),
					'article_modified ' => time()
				);
		$CI->db->insert('articles', $data);
		$data = array(
					'article_id' => 1,
					'category_id' => 1,
				);
		$CI->db->insert('article2cat', $data);
		$data = array(
					'cat_parent' => 0,
					'cat_name' => "Example Category",
					'cat_uri' => "example",
					'cat_description' => 'This is an example category.',
					'cat_display' => 'Y'
				);
		$CI->db->insert('categories', $data);
		$data = array(
					'g_term' => "68kb",
					'g_definition' => '68kb is a php knowledge base script. This is an example of the glossary function.'
				);
		$CI->db->insert('glossary', $data);
	}
}

/* End of file 001_Create_core.php */
/* Location: ./upload/includes/application/migrations/001_Create_core.php */ 